package cn.itcast.hotel.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 使用bean的方式创建交换机和队列并绑定
 * 还可以使用注解的方式
 */
@Configuration
public class MqConfig {

    /**
     * 创建topic交换机
     */
    @Bean
    public TopicExchange topicExchange(){
        //指定持久化存储
        return  new TopicExchange(cn.itcast.hotel.Constant.MqConfig.HOTEL_EXCHANGE,true,false);
    }

    /**
     * 创建insert队列
     */
    @Bean
    public Queue insertQueue(){
        return new Queue(cn.itcast.hotel.Constant.MqConfig.HOTEL_INSERT_QUEUE,true);
    }

    /**
     * 创建delete队列
     */
    @Bean
    public Queue deleteQueue(){
        return new Queue(cn.itcast.hotel.Constant.MqConfig.HOTEL_DELETE_QUEUE,true);
    }

    /**
     * 绑定交换机和insert队列
     */
    @Bean
    public Binding insertBinding(){
        return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(cn.itcast.hotel.Constant.MqConfig.HOTEL_INSERT_BINDINGKEY);
    }

    /**
     * 绑定交换机和delete队列
     */
    @Bean
    public Binding deleteBinding(){
        return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(cn.itcast.hotel.Constant.MqConfig.HOTEL_DELETE_BINDINGKEY);

    }

}
